package com.example.studentgradesmanagement.controller;

import com.example.studentgradesmanagement.model.Course;
import com.example.studentgradesmanagement.service.CourseService;
import com.example.studentgradesmanagement.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/listAll")
    public ResponseEntity<List<Course>> listAll() {
        List<Course> courses = courseService.listAll();
        return ResponseEntity.ok(courses);
    }

    @GetMapping("/getById/{courseId}")
    public ResponseEntity<ApiResponse> getById(@PathVariable Long courseId) {
        Optional<Course> course = courseService.getById(courseId);
        if (course.isPresent()) {
            return ResponseEntity.ok(new ApiResponse(true, "Course found", course.get()));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse> add(@RequestBody Course course) {
        courseService.add(course);
        return ResponseEntity.ok(new ApiResponse(true, "Course added successfully", course));
    }
}
